<template>
	<view class="search-results">
		<view v-show="showEmpty">
			<u-empty icon-size="152" font-size="40" margin-top="100" class="empty"></u-empty>
		</view>
		<view v-show="!showEmpty">
			<navigator v-for="(goal, index) in goalList" :key="index" :url="`/pages/insidePages/releaseDetails/index?from=home&userSignRecordId=${goal.userSignRecordId}`">
				<ReleaseCard :goalProp="goal" @like='handleLike' @userCenter='handleToUserCenter'></ReleaseCard>
			</navigator>
			
		</view>

		<!-- 加载完 -->
		<view v-show="pagination.totalCount >= goalList.length && !showEmpty" class="finished-loading">
			<u-divider>我是有底线的 凸^-^凸</u-divider>
		</view>

		<!-- 登录弹窗 -->
		<u-popup v-model="showLoginDialog" mode="center" width="500rpx">
			<view class="popup-wrapper">
				<view class="tip">
					<view>您凭证已过期或未登录</view>
					<view>请登录</view>
				</view>
				<u-button type="primary" :loading="isLogin" open-type="getUserInfo" @getuserinfo="handleLogin">登录</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import loginMixin from '@/utils/loginMixin.js'
	import ReleaseCard from '@/components/ReleaseCard/index.vue'
	import { parseTime } from '@/utils/index.js'
	import { getSignListAll } from '@/api/home.js'
	import { authCode2Session, decryptData, concern } from '@/api/user.js'
	import { clickLike } from '@/api/goal.js'

	export default {

		mixins: [loginMixin],
		
		components: {
			ReleaseCard
		},

		data() {
			return {
				defaultAvatar: require('@/static/user/default-avatar.png'),
				keyWord: '',
				goalList: [],
				showLoginDialog: false,
				isLoading: false,
				pagination: {
					startIndex: 0,
					length: 15,
					totalCount: 0,
				},
			}
		},

		onLoad(options) {
			this.keyWord = options.keyWord
			this.init()
		},

		// 下拉刷新
		onPullDownRefresh() {
			this.init()
		},

		// 上拉加载
		async onReachBottom() {
			if (this.pagination.totalCount >= this.goalList.length) return
			this.pagination.startIndex += 1
			await this.handleGetSignListAll()
		},
		
		computed: {
			showEmpty() {
				return this.goalList.length === 0
			}
		},

		methods: {
			
			init() {
				this.goalList = []
				this.pagination.startIndex = 0
				this.handleGetSignListAll()
			},
			
			// 跳转详情页
			handleToDetails(goal) {
				uni.navigateTo({
					url: `/pages/insidePages/releaseDetails/index?userSignRecordId=${goal.userSignRecordId}`
				})
			},
			
			// 跳转用户中心
			handleToUserCenter(goal) {
				// 点击的是自己的信息跳转个人中心
				if (this.user.openId === goal.openId) {
					uni.switchTab({
						url: '/pages/mine/index'
					})
				} else {
					uni.navigateTo({
						url: `/pages/insidePages/userCenter/index?openId=${goal.openId}&nickName=${goal.nickName}&avatarUrl=${goal.avatarUrl}&userConcern=${goal.userConcern}`
					})
				}
			},

			// 获取搜索结果
			async handleGetSignListAll() {
				const data = JSON.stringify({
					keyWord: this.keyWord,
					length: this.pagination.length,
					startIndex: this.pagination.startIndex,
					currentUserOpenId: (this.user && this.user.openId) ? this.user.openId : ''
				})
				try {
					uni.showLoading({
					    title: '加载中'
					});
					const {
						resultData
					} = await getSignListAll({
						data
					})
					this.goalList = [...this.goalList, ...resultData.list]
					this.pagination.totalCount = resultData.totalCount
					uni.stopPullDownRefresh()
					uni.hideLoading()
				} catch(e) {
					uni.hideLoading()
				}
			},
			
			// 发布列表的封面
			coverImg(goal) {
				return goal.signPic ? JSON.parse(goal.signPic)[0] : ''
			},

			// 查看图片
			handleExamineImage(goal) {
				const imgList = goal.signPic ? JSON.parse(goal.signPic) : ''
				if (imgList) {
					uni.previewImage({
						indicator: 'none',
						urls: imgList
					})
				}
			},

			/**
			 * 点赞
			 * 1. 检查是否已经登录
			 */
			async handleLike(goal) {
				if (!this.user.hasOwnProperty('openId')) {
					this.showLoginDialog = true
					return
				}
				try {
					const data = JSON.stringify({
						openId: this.user.openId,
						userSignRecordId: goal.userSignRecordId
					})
					const res = await clickLike({
						data
					})
					if (goal.userClick) {
						goal.userClick = !goal.userClick
						goal.totalClickCount -= 1
					} else {
						goal.userClick = !goal.userClick
						goal.totalClickCount += 1
					}

				} catch (e) {
					//TODO handle the exception
					if (e.resultCode === 701) {
						this.showLoginDialog = true
					}
				}
			}
		},

		filters: {
			timeStamp(value) {
				if (!value) return ''
				return parseTime(value)
			}
		}
	}
</script>

<style lang="scss">
	.finished-loading {
		padding-bottom: 40rpx;
	}

	.popup-wrapper {
		padding: 24rpx;
		text-align: center;

		.tip {
			padding: 50rpx 0;
		}
	}
</style>
